<?php
/**
 * Extended class to override the time_created
 * 
 * @property string $status      The published status of the blog post (published, draft)
 * @property string $comments_on Whether commenting is allowed (Off, On)
 * @property string $excerpt     An excerpt of the blog post used when displaying the post
 */
class ElggBlog extends ElggObject {

	/**
	 * Set subtype to blog.
	 */
	protected function initializeAttributes() {
		parent::initializeAttributes();

		$this->attributes['subtype'] = "blog";
		$this->attributes['title'] = NULL;
		$this->attributes['description'] = NULL;
		$this->attributes['receive_product_name'] = NULL;
		$this->attributes['receive_product_description'] = NULL;
		$this->attributes['exchange_product_category'] = NULL;
		$this->attributes['receive_product_category'] = NULL;
		$this->attributes['exchange_product_price'] = NULL;
		$this->attributes['exchange_money_type'] = NULL;
		$this->attributes['exchange_product_image'] = NULL;
		$this->attributes['receive_product_name'] = "a";	
	}

	/**
	 * Can a user comment on this blog?
	 *
	 * @see ElggObject::canComment()
	 *
	 * @param int $user_guid User guid (default is logged in user)
	 * @return bool
	 * @since 1.8.0
	 */
	public function canComment($user_guid = 0) {
		$result = parent::canComment($user_guid);
		if ($result == false) {
			return $result;
		}

		if ($this->comments_on == 'Off') {
			return false;
		}
		
		return true;
	}

	function customSave(){
	if (!ElggEntity::save()) {
			return false;
		}

		// Save ElggObject-specific attributes
register_error($this->get('title'));
		_elgg_disable_caching_for_entity($this->guid);
		$ret = ElggBlog::create_exchange_entity($this->get('guid'), $this->get('title'), $this->get('description'),
									  $this->get('receive_product_name'), $this->get('receive_product_description'),  $this->get('exchange_product_category'), 
									  $this->get('receive_product_category'), $this->get('exchange_product_price'), $this->get('exchange_money_type'), $this->get('exchange_product_image'));
		_elgg_enable_caching_for_entity($this->guid);

		return $ret;
	}
	function create_exchange_entity($guid, $exchangeName, $exchangeDescription, $receiveName, 
								$receiveDescription, $exchangeCategory, $receiveCategory, 
								$moneyPrice, $moneyType, $exchangeImage){
		global $CONFIG;
	$exchangeImage= "b";
	$guid = (int)$guid;
	$title = sanitise_string($title);
	$description = sanitise_string($description);

	$row = get_entity_as_row($guid);
	if ($row) {
		// Core entities row exists and we have access to it
		$query = "SELECT guid from {$CONFIG->dbprefix}objects_entity where guid = {$guid}";
		if ($exists = get_data_row($query)) {
			$query = "UPDATE {$CONFIG->dbprefix}objects_entity
				set title='$exchangeName', description='$exchangeDescription', receive_product_name='$receiveName', 
					receive_product_description= '$receiveDescription', exchange_product_category= '$exchangeCategory', 
					receive_product_category= '$receiveCategory', exchange_product_price= '$moneyPrice', 
					exchange_money_type= '$moneyType', exchange_product_image='$exchangeImage',
				where guid=$guid";

			$result = update_data($query);
			if ($result != false) {
				// Update succeeded, continue
				$entity = get_entity($guid);
				elgg_trigger_event('update', $entity->type, $entity);
				return $guid;
			}
		} else {
		;
			// Update failed, attempt an insert.
			$query = "INSERT into {$CONFIG->dbprefix}objects_entity
				(guid, title, description, receive_product_name, receive_product_description, 
				 exchange_product_category,receive_product_category, exchange_product_price, 
				 exchange_money_type,exchange_product_image) 
				values ($guid, '$exchangeName','$exchangeDescription', '$receiveName','$receiveDescription', 
						'$exchangeCategory','$receiveCategory', '$moneyPrice','$moneyType', '$exchangeImage')";

			$result = insert_data($query);
			if ($result !== false) {
				$entity = get_entity($guid);
				if (elgg_trigger_event('create', $entity->type, $entity)) {
					return $guid;
				} else {
					$entity->delete();
				}
			}
		}
	}

	return false;
	}
	
function setInformation($exchangeName, $exchangeDescription, $receiveName, 
                           $receiveDescription, $exchangeCategory, $receiveCategory, 
                           $moneyPrice, $moneyType, $exchangeImage){
						   $message = "";
						   $message= $this->isValidInformation($exchangeName, $exchangeDescription, $receiveName, 
															   $receiveDescription, $exchangeCategory, $receiveCategory, 
															   $moneyPrice, $moneyType, $exchangeImage);
						   if (!empty($message))
						   {
						   return $message;
						   }
						  // $this->attributes['type'] = "object";
							$this->attributes['title'] = $exchangeName;
							$this->attributes['description'] = $exchangeDescription;
							$this->attributes['receive_product_name'] = $receiveName;
							$this->attributes['receive_product_description'] = $receiveDescription;
							$this->attributes['exchange_product_category'] = $exchangeCategory;
							$this->attributes['receive_product_category'] = $receiveCategory;
							$this->attributes['exchange_product_price'] = $moneyPrice;
							$this->attributes['exchange_money_type'] = $moneyType;
							$this->attributes['exchange_product_image'] = $exchangeImage;
							return $message;
        
    }
function isValidInformation($exchangeName, $exchangeDescription, $receiveName, 
                           $receiveDescription, $exchangeCategory, $receiveCategory, 
                           $moneyPrice, $moneyType, $exchangeImage){
						   $messsage="";
						   if(empty($exchangeName)){ $message = elgg_echo('exchange:name:empty');
										return $message;		
						   }
						    if(empty($exchangeDescription)){ $message = elgg_echo('exchange:description:empty');
										return $message;							
						   }
						   if(empty($receiveName)){ $message = elgg_echo('receive:name:empty');
										return $message;		
						   } 
						   if(empty($receiveDescription)){ $message = elgg_echo('receive:description:empty');
										return $message;		
						   } 
						   if(empty($exchangeCategory)){ $message = elgg_echo('exchange:category:empty');
										return $message;		
						   } 
						   if(empty($receiveCategory)){ $message = elgg_echo('receive:category:empty');
										return $message;		
						   } 
						   if(empty($moneyPrice)){ $message = elgg_echo('money:price:empty');
										return $message;		
						   }
							if (!is_numeric($moneyPrice))
							{
								$message = elgg_echo('money:only:number');
							}
						   if(empty($moneyType)){ $message = elgg_echo('money:type:empty');
										return $message;		
						   }
						   /*if(empty($exchangeImage)){ $message = elgg_echo('exchange:image:empty');
										return $message;		
						   }*/
                           return $message;
                           return $message;
}
}

